Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

435
Views
Javascript: How to convert array ["a=1", "b=2"] into an object?

What is the best way to convert:

user = ['Name:Marry', 'Age:24', 'Gender:Female']

to

user = { 'Name':'Marry', 'Age':'24', 'Gender':'Female' }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

One line solution:

const result = Object.assign({}, ...user.map(item => item.split(':')).map(item => ({ [item[0]]: item[1]})))
about 4 years ago · Juan Pablo Isaza Report

0

    user = ['Name:Marry', 'Age:24', 'Gender:Female']
    const userObj = {}

    for(item of user) {
      const [key, value] = item.split(":")
      userObj[key] = value
    }
    
    console.log(userObj)

about 4 years ago · Juan Pablo Isaza Report

0

Here you want to convert an array to a single value( here it is an object ), for this more declarative way is to use reduce method. Here I have given an empty object as the initial value of the accumulator and on each iteration it adds the key value pair.

const result = user.reduce((acc, curr) => {
  const [key, value] = curr.split(':');
  acc[key] = value;
  
  return acc;
}, {});

Hope this helps.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!